package com.leon.datalink.node.actor;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import com.leon.datalink.core.exception.impl.DataValidateException;
import com.leon.datalink.core.monitor.ListenerContent;
import com.leon.datalink.core.monitor.ListenerTypeEnum;
import com.leon.datalink.core.utils.SnowflakeIdWorker;
import com.leon.datalink.node.AbstractNodeActor;
import com.leon.datalink.node.context.NodeContext;
import com.leon.datalink.node.context.Output;
import com.leon.datalink.node.filter.CorsFilter;
import com.leon.datalink.node.response.ResponseManager;
import com.leon.datalink.resource.util.http.SimpleHttpServer;
import com.sun.net.httpserver.HttpExchange;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class HttpServerNodeActor extends AbstractNodeActor {

    SimpleHttpServer simpleHttpServer;

    @Override
    public void create(NodeContext context, Output output) throws Exception {
        Integer port = properties.getInteger("port");
        List<String> paths = properties.getStringList("paths");
        if (null == port) throw new DataValidateException();
        if (CollectionUtil.isEmpty(paths)) throw new DataValidateException();

        simpleHttpServer = new SimpleHttpServer(port)
                .addFilter(new CorsFilter());

        int index = 1;
        for (String path : paths) {
            path = StrUtil.addPrefixIfNot(path, "/");

            final String finalPath = path;
            final int portIndex = index++;

            simpleHttpServer.addHandler(finalPath, httpExchange -> {
                HttpServerRequest req = new HttpServerRequest(httpExchange);

                // 路径校验
                // 必须完全匹配，避免误触 /a/xxx 命中 /a
                if (!req.getPath().equals(finalPath)) {
                    String msg = "404 Not Found";
                    byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);

                    // 设置 404 状态码 和响应体长度
                    httpExchange.sendResponseHeaders(404, bytes.length);
                    httpExchange.getResponseBody().write(bytes);
                    httpExchange.getResponseBody().close();
                    return;
                }

                String requestId = SnowflakeIdWorker.getId();
                ResponseManager.add(requestId, httpExchange);
                context.put("requestId", requestId);

                Map<String, Object> result = new HashMap<>();
                result.put("path", req.getPath());
                result.put("method", req.getMethod());
                result.put("headers", req.getHeaders());
                result.put("body", req.getBody());
                result.put("params", req.getParams());
                output.out(portIndex, result);
            });
        }

        simpleHttpServer.start();
        ListenerContent.add(port, ListenerTypeEnum.TCP, "HTTP server node port");
    }

    @Override
    public void destroy() throws Exception {
        if (null != simpleHttpServer) simpleHttpServer.stop(0);
        Integer port = properties.getInteger("port");
        if (null == port) throw new DataValidateException();
        ListenerContent.remove(port);
    }




}
